package com.wontube.smr.cron;

import java.util.HashSet;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.wontube.core.cron.IJobs;
import com.wontube.smr.bo.sites.hulu.HuluObtainBO2;


/**
 * @author yuechao
 * 
 */
public class ObtainHuluJobs implements IJobs {
	private static final Logger logger = LoggerFactory.getLogger(ObtainHuluJobs.class);
	
	/* 
	 * @see core.cron.IJobs#name()
	 */
	@Override
	public String name() {
		return "obtain-hulu";
	}

	/* 
	 * @see core.cron.IJobs#execute()
	 */
	@Override
	public IJobs execute(String[] args) {
		// 防止多次指定相同的任务
		HashSet<String> set = new HashSet<String>();
		for (String arg : args) {
			set.add(arg);
		}
		
		if (set.contains("-movie")) {
			this.obtainPopularMovies();
		}
		
		if (set.contains("-update_movie")) {
			this.updatePopularMovies();
		}
		
		if (set.contains("-recent_movie")) {
			this.obtainRecentMovies();
		}
		
		/*
		if (set.contains("-popular_games")) {
			this.obtainPopularGames();
		}
		
		if (set.contains("-week_movie")) {
			this.obtainPopularWeekMovies();
		}
		
		if (set.contains("-recent_games")) {
			this.obtainRecentGames();
		}
		*/
		
		if (set.contains("-tv")) {
			this.obtainPopularTV();
		}
		
		if (set.contains("-update_tv")) {
			this.updatePopularTV();
		}
		
		if (set.contains("-recent_tv")) {
			this.obtainRecentTV();
		}
		
		
		if (set.contains("-popular_episode")) {
			this.obtainPopularEpisodes();
		}
		
		if (set.contains("-recent_episode")) {
			this.obtainRecentEpisodes();
		}

		/*
		if (set.contains("-score_time_tv")) {			
			this.scoreAndTimeTV();
		}
		*/
		return this;
	}

	private void obtainPopularMovies() {
		long start = System.currentTimeMillis();
		int count = 0;
		try {
			count = HuluObtainBO2.grabAllPopularMovies();
		} catch (Exception e) {
			logger.error("error happen when grab Hulu all Popular Movies", e);
		}
		
		long cost = System.currentTimeMillis() - start;
		cost /= 1000;
		logger.info("obtain {} Hulu Popular Movie, cost {} s", count, cost);
	}
	
	private void updatePopularMovies(){
		long start = System.currentTimeMillis();
		int count = 0;
		try {
			count = HuluObtainBO2.updatePopularMovies();
		} catch (Exception e) {
			logger.error("error happen when update Hulu Popular Movies", e);
		}
		
		long cost = System.currentTimeMillis() - start;
		cost /= 1000;
		logger.info("update {} Hulu popular Movie, cost {} s", count, cost);
	}
	
	private void obtainRecentMovies() {
		long start = System.currentTimeMillis();
		int count = 0;
		try {
			count = HuluObtainBO2.updateRecentMovies();
		} catch (Exception e) {
			logger.error("error happen when grab Hulu Recent Movies", e);
		}
		
		long cost = System.currentTimeMillis() - start;
		cost /= 1000;
		logger.info("obtain {} Hulu Recent Movie items, cost {} s", count, cost);
	}	
	
	private void obtainPopularTV() {
		long start = System.currentTimeMillis();
		int count = 0;
		try {
			count = HuluObtainBO2.grabAllPopularTVShows();
		} catch (Exception e) {
			logger.error("error happen when grab all Hulu Popular TV shows", e);
		}
		long cost = System.currentTimeMillis() - start;
		cost /= 1000;
		logger.info("obtain {} Hulu Popular TV Shows, cost {} s", count, cost);
	}
	
	private void updatePopularTV() {
		long start = System.currentTimeMillis();
		int count = 0;
		try {
			count = HuluObtainBO2.updatePopularTVShows();
		} catch (Exception e) {
			logger.error("error happen when update Hulu Popular TV Show", e);
		}
		long cost = System.currentTimeMillis() - start;
		cost /= 1000;
		logger.info("update {} Hulu Popular TV Shows, cost {} s", count, cost);
	}
	
	private void obtainRecentTV() {
		long start = System.currentTimeMillis();
		int count = 0;
		try {
			count = HuluObtainBO2.updateRecentTVShows();
		} catch (Exception e) {
			logger.error("error happen when grab Hulu Recent TV Shows", e);
		}
		long cost = System.currentTimeMillis() - start;
		cost /= 1000;
		logger.info("obtain {} Hulu Recent TV Shows, cost {} s", count, cost);
	}
	
	private void obtainPopularEpisodes(){
		long start = System.currentTimeMillis();
		int count = 0;
		try {
			count = HuluObtainBO2.updatePopularEpisodes();
		} catch (Exception e) {
			logger.error("error happen when grab Hulu Popular TV episodes	", e);
		}
		long cost = System.currentTimeMillis() - start;
		cost /= 1000;
		logger.info("update {} Hulu Popular TV Episodes, cost {} s", count, cost);
	}
	
	private void obtainRecentEpisodes() {
		long start = System.currentTimeMillis();
		int count = 0;
		try {
			count = HuluObtainBO2.updateRecentTVEpisodes();
		} catch (Exception e) {
			logger.error("error happen when grab Hulu recent TV episodes	", e);
		}
		long cost = System.currentTimeMillis() - start;
		cost /= 1000;
		logger.info("update {} Hulu recent Episodes, cost {} s", count, cost);
	}	
}
